package com.jz.community.moduleshopping.coupon.ui;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jz.community.basecomm.base.BaseMvpFragment;
import com.jz.community.basecomm.mvp.BaseMvpPresenter;
import com.jz.community.basecomm.routerUtils.RouterCommonUtils;
import com.jz.community.basecomm.routerUtils.RouterIntentConstant;
import com.jz.community.basecomm.task.ITaskCallbackListener;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.basecomm.utils.WpToast;
import com.jz.community.basecomm.utils.eventbus.AppEvent;
import com.jz.community.basecomm.utils.eventbus.EventConfig;
import com.jz.community.commview.callback.EmptyCallback;
import com.jz.community.moduleshopping.R;
import com.jz.community.moduleshopping.R2;
import com.jz.community.moduleshopping.coupon.adapter.CouponListAdapter;
import com.jz.community.moduleshopping.coupon.task.GetCouponListTask;
import com.jz.community.moduleshopping.goodsDetail.bean.GoodsCouponInfo;
import com.kingja.loadsir.core.LoadService;
import com.kingja.loadsir.core.LoadSir;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Tony.Fan on 2018/6/06 14:16
 * 优惠卷 可用、历史 列表
 */
public class CouponListFragment extends BaseMvpFragment implements OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener {

    @BindView(R2.id.coupon_list_rv)
    RecyclerView couponListRv;
    @BindView(R2.id.coupon_layout)
    LinearLayout couponLayout;
    @BindView(R2.id.coupon_list_refresh)
    SmartRefreshLayout couponListRefresh;

    private static int couponType;//0 可用 1 历史

    private CouponListAdapter adapter_coupon;
    private LoadService loadService;

    private int page = 0;//页数
    private GoodsCouponInfo goodsCouponInfo;

    public static CouponListFragment newInstance(int type) {
        couponType = type;
        CouponListFragment couponFragment = new CouponListFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("couponType", couponType);
        couponFragment.setArguments(bundle);
        return couponFragment;
    }


    @Override
    protected int getContentViewLayoutID() {
        return R.layout.module_shopping_fragment_coupon;
    }

    @Override
    protected BaseMvpPresenter createPresenter() {
        return null;
    }

    @Override
    protected void initDatas() {
        super.initDatas();
        loadService = LoadSir.getDefault().register(couponLayout);
        loadService.setCallBack(EmptyCallback.class, (context, view) -> {
            ImageView empty_iv = view.findViewById(R.id.empty_img);
            TextView empty_tv = view.findViewById(R.id.empty_tv);
            empty_iv.setImageResource(R.mipmap.empty_default);
            empty_tv.setText("您还没有优惠券哦！");
        });
    }

    @Override
    protected void initLazy(@Nullable Bundle savedInstanceState) {
        couponType = getArguments().getInt("couponType");
        handleBindCouponListAdapter();
        getCouponData(true);
    }

    private void getCouponData(boolean isRefresh) {
        new GetCouponListTask(getActivity(), new ITaskCallbackListener() {
            @Override
            public void doTaskComplete(Object o) {
                goodsCouponInfo = (GoodsCouponInfo) o;
                if (!Preconditions.isNullOrEmpty(goodsCouponInfo) && !Preconditions.isNullOrEmpty(goodsCouponInfo.get_embedded().getContent())) {
                    loadService.showSuccess();
                    couponListRefresh.finishRefresh();
                    setData(isRefresh, goodsCouponInfo.get_embedded().getContent());
                } else {
                    loadService.showCallback(EmptyCallback.class);
                }
            }
        }).execute(couponType + "", page + "");
    }

    private void handleBindCouponListAdapter() {
        LinearLayoutManager main_layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        couponListRv.setLayoutManager(main_layoutManager);
        couponListRefresh.setOnRefreshListener(this);
        adapter_coupon = new CouponListAdapter(R.layout.module_shopping_item_coupon_list, new ArrayList<>(), couponType);
        adapter_coupon.setEnableLoadMore(false);
        adapter_coupon.setOnLoadMoreListener(this, couponListRv);
        adapter_coupon.openLoadAnimation(BaseQuickAdapter.ALPHAIN);//加载动画
        couponListRv.setAdapter(adapter_coupon);
        adapter_coupon.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.coupon_list_click_tv) {

                    if (adapter_coupon.getData().get(position).getHistoryStatus() ==5){
                        WpToast.l(getActivity(),"该优惠券还未到使用时间");
                        return;
                    }
                    RouterCommonUtils.startNoParameterActivity(RouterIntentConstant.APP_MODULE_MAIN);
                    EventBus.getDefault().post(new AppEvent(EventConfig.HOME));
                }
            }
        });
    }

    private void setData(boolean isRefresh, List data) {

        page++;

        if (isRefresh) {
            adapter_coupon.setNewData(data);
        } else {
            adapter_coupon.addData(data);
        }

        if (goodsCouponInfo.getPage().getTotalPages() <= page) {
            adapter_coupon.loadMoreEnd();
        } else {
            adapter_coupon.loadMoreComplete();
        }
    }


    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        page = 0;
        getCouponData(true);
    }

    @Override
    public void onLoadMoreRequested() {
        getCouponData(false);
    }
}
